package com.zzy.forum3.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.sql.ResultSet;

public class AppResult<T> {
    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private Long code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String msg;
    //返回的具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public AppResult(Long code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public AppResult(Long code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(),message);
    }

    public static<T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    public static<T> AppResult<T> success(String message, T data){
        return new AppResult(ResultCode.SUCCESS.getCode(),message,data);
    }

    //下面的表示的都是失败的信息
    public static AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }

    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }

    public Long getCode() {
        return code;
    }

    public void setCode(Long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
